DWORREC ;PICASSO/JOLLIS - ORMS RECORD ROUTINES;9/11/10;src/orms/DWORREC.m
;;3.0;DWORMS;9/11/10;1
;; $Id: DWORREC.m 4 2010-10-24 17:30:25Z jollis $

;;PDOC
;;SUMMARY Create a new ORMS record
;;DEFFCN
CREATE(PARENT,CLASS,NAME,KEY,DATA)		
;;ARG 1 PARENT The parent record of this record
;;ARG 2 CLASS The CLASNAME of this record
;;ARG 3 NAME The name of this record
;;ARG 4 KEY The key of this record
;;ARG 5 DATA The data contained in this record
;;PDOC/
 S OID=$$GETNEXT^DWKRFUID()
 TS
 S REV=0
 S ^DWORMS(OID)=1
 S ^DWORMS(OID,"PAR")=PARENT
 S ^DWORMS(OID,"CLS")=CLASS
 S ^DWORMS(OID,KEY,REV,"DAT")=DATA
 S ^DWORMS(OID,"NAM")=NAME
 S ^DWORMS(OID,KEY,REV,"DTM")=$H
 S ^DWORMS(OID,KEY,REV,"JOB")=$J
 S ^DWORMS(OID,KEY,"NEW")=REV
 S ^DWORSTAT("ADD")=^DWORSTAT("ADD")+1
 S ^DWORSTAT("PDBBYTES")=^DWORSTAT("PDBBYTES")+$L(DATA)
 S ^DWORSTAT("KEYBYTES")=^DWORSTAT("KEYBYTES")+$L(KEY)
 S ^DWORSTAT("OIDBYTES")=^DWORSTAT("OIDBYTES")+$L(OID)
 TC
 I KEY=$$GETPK^DWORCLAS(CLASS) D SIDX3^DWORINDX(CLASS,KEY,DATA,OID)
 I ^DWORMS(OID,"CLS")'="ROOTNODE" D
 .D RUNHOOKS^DWORHOOK(CLASS,"CREATE",OID,KEY,REV,DATA)
 Q OID


;;PDOC
;;SUMMARY Write to an existing ORMS record
;;DEFSUB
OUTPUT(OID,KEY,DATA)
;;ARG 1 OID The OID of the record
;;ARG 2 KEY The key of the record
;;ARG 3 DATA The PDB of the record
;;PDOC/
 I $$ISLOCKED^DWORREC(OID) Q
 S REV=^DWORMS(OID,KEY,"NEW")+1
 S OREV=^DWORMS(OID,KEY,"NEW")
 TS
 S ^DWORMS(OID,KEY,"NEW")=REV
 S ^DWORMS(OID,KEY,REV,"DAT")=DATA
 S ^DWORMS(OID,KEY,REV,"DTM")=$H
 S ^DWORMS(OID,KEY,REV,"JOB")=$J
 S ^DWORSTAT("REVISE")=^DWORSTAT("REVISE")+1
 S ^DWORSTAT("PDBBYTES")=^DWORSTAT("PDBBYTES")+$L(DATA)
 TC
 S CLS=^DWORMS(OID,"CLS")
 I KEY=$$GETPK^DWORCLAS(CLS) D SIDX3^DWORINDX(CLS,KEY,DATA,OID)
 D RUNHOOKS^DWORHOOK($G(^DWORMS(OID,"CLS")),"REVISE",OID,KEY,REV,DATA)
 Q

;;PDOC
;;SUMMARY Append to an existing ORMS record
;;DEFSUB
APPEND(OID,KEY,DATA)
;;ARG 1 OID OID
;;ARG 2 KEY Key
;;ARG 3 DATA PDB
;;PDOC/
 I $$ISLOCKED^DWORREC(OID) Q
 I '$G(^DWORMS(OID,KEY,"NEW")) D
 .TS
 .S ^DWORMS(OID,KEY,"NEW")=0
 .S ^DWORMS(OID,KEY,$$NEWEST^DWORREC(OID,KEY),"DAT")=DATA
 .S ^DWORMS(OID,KEY,$$NEWEST^DWORREC(OID,KEY),"DTM")=$H
 .S ^DWORMS(OID,KEY,$$NEWEST^DWORREC(OID,KEY),"JOB")=$J
 .S ^DWORSTAT("APPEND")=^DWORSTAT("APPEND")+1
 .S ^DWORSTAT("PDBBYTES")=^DWORSTAT("PDBBYTES")+$L(DATA)
 .S ^DWORSTAT("KEYBYTES")=^DWORSTAT("KEYBYTES")+$L(KEY)
 .S ^DWORSTAT("OIDBYTES")=^DWORSTAT("OIDBYTES")+$L(OID)
 .TC
 .S REV=$$NEWEST^DWORREC(OID,KEY)
 .S CLS=^DWORMS(OID,"CLS")
 I KEY=$$GETPK^DWORCLAS(CLS) D SIDX3^DWORINDX(CLS,KEY,DATA,OID)
 .D RUNHOOKS^DWORHOOK($G(^DWORMS(OID,"CLS")),"REVISE",OID,KEY,REV,DATA)
 Q

;;PDOC
;;SUMMARY Read from an existing ORMS record
;;DEFFCN
INPUT(OID,KEY,REV)
;;ARG 1 OID OID
;;ARG 2 KEY Key
;;ARG 3 REV Revision
;;PDOC/
 S RESLT=$G(^DWORMS(OID,KEY,REV,"DAT"))
 S ^DWORSTAT("READS")=^DWORSTAT("READS")+1
 S ^DWORSTAT("READBYTES")=^DWORSTAT("READBYTES")+$L(RESLT)
 Q RESLT

;;PDOC
;;SUMMARY Get by class and key
;;DEFFCN
CLSGET(CLASS,KEY,DATA)
;;ARG 1 CLASS CLASNAME
;;ARG 2 KEY Key
;;ARG 3 DATA PDB
;;PDOC/
 S OID=$$LIDX3^DWORINDX(CLASS,KEY,DATA)
 Q $$INPUT^DWORREC(OID,KEY,$$NEWEST^DWORREC(OID,KEY))

;;PDOC
;;SUMMARY Get an OID through ^DWIND
;;DEFFCN
INDGET(CLASS,KEY,DATA)
;;ARG 1 CLASS CLASNAME
;;ARG 2 KEY Key
;;ARG 3 DATA PDB
;;PDOC/
 Q $$LIDX3^DWORINDX(CLASS,KEY,DATA)

;;PDOC 
;;SUMMARY Get the newest revision number for record OID,KEY
;;DEFFCN
NEWEST(OID,KEY)
;;ARG 1 OID OID
;;ARG 2 KEY Key
;;PDOC/
 Q $G(^DWORMS(OID,KEY,"NEW"))

;;PDOC
;;SUMMARY Set a lock on record OID
;;DEFSUB
SETLOCK(OID)
;;ARG 1 OID OID
;;PDOC/
 I $$ISLOCKED^DWORREC(OID) Q
 TS
 S ^DWLCKS(OID)=$J
 TC
 Q

;;PDOC
;;SUMMARY Cull revisions for OID, preserving at most COUNT revisions
;;DEFSUB
CULL(OID,COUNT)
;;ARG 1 OID OID of object to cull
;;ARG 2 COUNT Maximum revisions to preserve
;;PDOC/
 Q

;;PDOC
;;SUMMARY Check for locks on record OID
;;DEFFCN
ISLOCKED(OID)
;;ARG 1 OID The OID of the desired record
;;PDOC/
 I $G(^DWLCKS(OID))'=$J Q "LOCKED"
 Q ""

;;PDOC
;;SUMMARY Clear locks on record OID
;;DEFSUB
CLRLOCK(OID)
;;ARG 1 OID The OID of the object to have its locks cleared
;;PDOC/
 TS
 I $$ISLOCKED^DWORREC(OID) K ^DWLCKS(OID)
 Q

;;PDOC
;;SUMMARY Get the root node for this instance
;;DEFFCN
ROOTNODE()
;;PDOC/
 Q $G(^DWPREF("ROOTNODE"))

;;PDOC
;;SUMMARY Sets the root node for this instance
;;DEFSUB
SETRTNOD(OID) 
;;ARG 1 OID The OID of the new root node
;;PDOC/
 S ^DWPREF("ROOTNODE")=OID
 Q

;;PDOC
;;SUMMARY returns 1 if OID exists, 0 if it does not
;;DEFFNC
RECEXIST(OID)
;;ARG 1 OID The OID of the record to check
;;PDOC/
 I $G(^DWORMS(OID))=1 Q 1
 I $G(^DWORMS(OID))=0 Q 0
 Q 0

;;PDOC
;;SUMMARY Get the class component of a computed field
;;DEFFNC
CFCLASS(CF)
;;ARG 1 CF The entire computed field
;;PDOC/
 S CLASPART=$P(CF,"(",1)
 Q $P(CLASPART,"$",2)

;;PDOC
;;SUMMARY Get the key component of a computed field
;;DEFFNC
CFKEY(CF)
;;ARG 1 CF The entire computed field
;;PDOC/
 S KEYPART=$P(CF,"(",2)
 Q $P(KEYPART,")",1)

;;PDOC
;;SUMMARY Get the field component of a computed field
;;DEFFNC
CFFIELD(CF)
;;ARG 1 CF The entire computed field
;;PDOC/
 S FIELDPRT=$P(CF,")",2)
 Q $P(FIELDPRT,".",2)

;;PDOC
;;SUMMARY Get a computed field's value
;;DEFFNC
CFGET(CF)
;;ARG 1 CF The entire computed field
;;PDOC/
 S CLASPART=$$CFCLASS(CF)
 S KEYPART=$$CFKEY(CF)
 S FIELDPRT=$$CFFIELD(CF)
 S PK=$$GETPK^DWORCLAS(CLASPART)
 ;;W "CLASPART=",CLASPART," KEYPART=",KEYPART," FIELDPRT=",FIELDPRT," PK=",PK,!
 S CLASNODE=$$INDGET^DWORREC(CLASPART,PK,KEYPART)
 ;;W "-",CLASNODE,"-",!
 I CLASNODE'="" D
 .S VAL=$$INPUT^DWORREC(CLASNODE,FIELDPRT,$$NEWEST^DWORREC(CLASNODE,FIELDPRT))
 I CLASNODE="" D
 .S VAL=""
 Q VAL

;;PDOC
;;SUMMARY Dump an ORMS record
;;DEFSUB
DUMP(OID)
;;ARG 1 OID The OID of the record to dump
;;PDOC/
 ZWRITE ^DWORMS(OID,*)
 Q

 	